package com.lihepeng.leecode.recursive;

import org.junit.Test;

/**
 * 使用递归函数求n的阶乘
 */
public class Factorial {
    public int factorial(int n) {
        int temp = 1;
        if (n >= 0) {
            for (int i = 1; i <= n; i++) {
                temp = temp * i;
            }
        } else {
            return -1;
        }
        return temp;
    }

    public int factorial2(int n) {
        if (n == 1) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }

    }

    @Test
    public void runTest() {
        int factorial = factorial(4);
        System.out.println(factorial);
    }

    @Test
    public void runTest2() {
        int i = factorial2(4);
        System.out.println(i);
    }
}

